package org.example.chapter3.service.iml;

import org.example.chapter3.entity.User;
import org.example.chapter3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceIml  implements UserService {
//    @Autowired
    JdbcTemplate jdbcTemplate;

    public UserServiceIml(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //增加
    @Override
    public int addUser(User user) {
        String sql = "insert into user (username,password) value (?,?)";
        return jdbcTemplate.update(sql,user.getUsername(),user.getPassword());
    }

    //修改
    @Override
    public int updateUser(User user) {
        String sql = "update user set username=?,password=?";
        Object []args = {user.getUsername(),user.getPassword()};
        return jdbcTemplate.update(sql,args);

    }

    //删除
    @Override
    public int deleteUser(int id) {
        String sql = "delete from user where id=?";
        return jdbcTemplate.update(sql,id);
    }

    //查询
    @Override
    public User findUser(int id) {
        String sql = "select * from user where id=?";
        User user= jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class));
        return user;
    }
}
